﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AzureTosser.Services;
using System.Net;

namespace AzureTosser.Controllers
{
    [JSONPFilter]
    public class RegisterController : Controller
    {
        public ActionResult Index(string type, string scope, string topic)
        {
            try
            {
                ITossBus provider = TossProviderFactory.Instance.Bus;
                PipeType pipeType = PipeTypes.GetPipeType(type);
                if (pipeType == PipeType.Receive)
                    return Json(provider.RegisterReceiverPipe(scope, topic), JsonRequestBehavior.AllowGet);
                else 
                    return Json(provider.RegisterSenderPipe(pipeType, scope), JsonRequestBehavior.AllowGet);
            }
            catch (ArgumentException argExp)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(argExp.Message, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return Json("Internal service error.", JsonRequestBehavior.AllowGet);
            }
        }
    }
}
